package com.hanbang.maige.view.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hanbang.maige.R;
import com.hanbang.maige.code.base.baseadapter.ViewHolder;
import com.hanbang.maige.code.base.baseadapter.recyclerview.CommonAdapter;
import com.hanbang.maige.code.base.view.activity.BaseActivity;
import com.hanbang.maige.code.base.view.activity.BaseMvpActivity;
import com.hanbang.maige.mode.javabean.login.BiaoQianData;
import com.hanbang.maige.pressenter.othre.BiaoQianPresenter;
import com.hanbang.maige.view.login.ILogInBaseView;
import com.hanbang.maige.widget.gridlayout.SuperFlexboxLayout;
import com.hanbang.maige.widget.sticky_recycler.CommonHeaderAdapter;
import com.hanbang.maige.widget.sticky_recycler.StickyHeadersBuilder;
import com.hanbang.maige.widget.sticky_recycler.StickyHeadersItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作者　　: 李坤
 * 创建时间:2016/10/25　9:58
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：
 */

public class BiaoQianActivity extends BaseMvpActivity<ILogInBaseView.IBiaoQianView, BiaoQianPresenter>
        implements ILogInBaseView.IBiaoQianView {
    ArrayList<BiaoQianData> listDatas = new ArrayList();
    @BindView(R.id.switchRoot)
    RecyclerView switchRoot;
    private StickyHeadersItemDecoration stickyHeaderItem;

    public static void startUI(BaseActivity activity) {
        Intent intent = new Intent(activity, BiaoQianActivity.class);
        activity.startActivityForResult(intent, activity.REQUEST_CODE);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_biaoqian;
    }

    @Override
    public void initView() {
        toolbar.setBack(this);

        switchRoot.setLayoutManager(new LinearLayoutManager(this));
        int dp10 = getResources().getDimensionPixelOffset(R.dimen.dp_10);
        stickyHeaderItem = new StickyHeadersBuilder()
                .setAdapter(adapter)
                .setRecyclerView(switchRoot)
                .setStickyHeadersAdapter(headerAdapter)
                .build();
        switchRoot.addItemDecoration(stickyHeaderItem);

        switchRoot.setPadding(dp10, dp10, dp10, dp10);
        switchRoot.setAdapter(adapter);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter.getHttp();
    }

    @Override
    public void setBiaoQian(List<BiaoQianData> childLeiBeen) {
        listDatas.addAll(childLeiBeen);
        adapter.notifyDataSetChanged();
    }

    @Override
    public BiaoQianPresenter initPressenter() {
        return new BiaoQianPresenter();
    }


    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public void clearData() {
        listDatas.clear();
    }

    private CommonHeaderAdapter headerAdapter = new CommonHeaderAdapter<BiaoQianData>(this, R.layout.item_biaoqian_head, listDatas) {
        @Override
        public void convert(ViewHolder holder, BiaoQianData o) {
            ((TextView) holder.getConvertView()).setText(o.getHyTypeName());
        }

        @Override
        public long getHeaderId(int position) {
            return listDatas.get(position).getHyID();
        }
    };


    CommonAdapter adapter = new CommonAdapter<BiaoQianData>(this, R.layout.item_biaoqian, listDatas) {
        @Override
        public void convert(ViewHolder holder, BiaoQianData o) {
            final SuperFlexboxLayout f = ((SuperFlexboxLayout) holder.getConvertView());
            if (f.isFormData(o.getChildLei())) {
                f.getAdapter().notifyDataSetChanged();
            } else {
                f.setAdapter(new CommonAdapter<BiaoQianData.ChildLeiBean>(mContext, R.layout.item_biaoqian_child, o.getChildLei()) {
                    @Override
                    public void convert(ViewHolder holder, BiaoQianData.ChildLeiBean o) {
//                        DrawableUtils.setBackgroundCompat(holder.getConvertView(), DrawableUtils.getStateListDrawable(
//                                R.color.main_color, R.color.gray, 4, 0, R.color.translucent
//                        ));
                        ((TextView) holder.getConvertView()).setText(o.getHyValueName());
                    }
                });
                f.setOnItemClickListener(new SuperFlexboxLayout.OnItemClickListener<BiaoQianData.ChildLeiBean>() {
                    @Override
                    public void onItemClick(SuperFlexboxLayout parent, View view, int position, BiaoQianData.ChildLeiBean data) {
                        Intent intent = new Intent();
                        intent.putExtra("data", data);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
            }
        }
    };


}
